package com.ndhutu.ee.util.netease.vod;

import com.alibaba.fastjson.JSONObject;
import com.ndhutu.ee.util.netease.App;
import com.ndhutu.ee.util.netease.NeteaseUtil;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 点播工具类
 * @Author Shane
 * @Date 2020/7/28 12:53
 */
public class VodUtil {
    private static Logger log = LoggerFactory.getLogger(VodUtil.class);

    private static Map<String, Object> post(String url, Map<String, Object> params, Map<String, String> headers) throws Exception{
        RequestBody body = RequestBody.create(MediaType.parse(NeteaseUtil.contentType_JSON), JSONObject.toJSONString(params));

        Request.Builder builder = new Request.Builder();
        for(Map.Entry<String, String> entry : headers.entrySet()){
            builder.addHeader(entry.getKey(), entry.getValue());
        }
        builder = builder.url(url).post(body);

        Request request = builder.build();
        Call call = new OkHttpClient().newCall(request);
        return JSONObject.parseObject(call.execute().body().string());
    }

    /**
     * 查询视频文件信息列表
     * @Author Shane
     * @Date 2020/7/28 13:10
     * @param page
     * @param limit
     * @param beginTime
     * @param endTime
     * @return
     */
    public static List<Map<String, Object>> list(Integer page, Integer limit, Date beginTime, Date endTime, App app) throws Exception{
        String url = "https://vcloud.163.com/app/vod/video/list";

        Map<String, Object> params = new HashMap<>();
        params.put("currentPage", page);
        params.put("pageSize", limit);
        if(beginTime != null){
            params.put("beginTime", beginTime.getTime());
        }
        if(endTime != null){
            params.put("endTime", endTime.getTime());
        }
        params.put("status", 0);
        params.put("type", 0);
        params.put("sortStr", "asc");

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            log.error(JSONObject.toJSONString(map));
            throw new Exception("查询视频文件信息列表失败");
        }else{
            Map<String, Object> ret = (Map)map.get("ret");
            return (List<Map<String, Object>>) ret.get("list");
        }
    }

    /**
     * 删除视频源文件
     * @Author Shane
     * @Date 2020/7/28 13:18
     * @param vid   视频id
     * @return
     */
    public static void videoDelete(Long vid, App app) throws Exception{
        String url = "https://vcloud.163.com/app/vod/video/videoDelete";

        Map<String, Object> params = new HashMap<>();
        params.put("vid", vid);

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            log.error(JSONObject.toJSONString(map));
            throw new Exception("删除视频源文件失败");
        }
    }

    /**
     * 批量删除视频
     * @Author Shane
     * @Date 2020/7/28 13:22
     * @param list  视频源文件信息集合，注意：一次最多删除50个
     * @return
     */
    public static void delete(List<Map<String, Object>> list, App app) throws Exception{
        String url = "https://vcloud.163.com/app/vod/video/delete";

        List<Long> vids = new ArrayList<>();
        for(Map<String, Object> map : list){
            vids.add(Long.parseLong(map.get("vid")+""));
        }

        Map<String, Object> params = new HashMap<>();
        params.put("vids", vids);

        Map<String, String> headers = NeteaseUtil.getRequestHeaders(app);

        Map<String, Object> map = post(url, params, headers);

        if(Integer.parseInt(map.get("code")+"") != 200){
            log.error(JSONObject.toJSONString(map));
            throw new Exception("批量删除视频失败");
        }
    }
}
